package com.ie.cloud.payment.controller;

import cn.hutool.extra.servlet.ServletUtil;
import com.alibaba.fastjson.JSONObject;
import com.ie.cloud.payment.config.aop.MethodLog;
import com.ie.cloud.payment.controller.base.CommonCtrl;
import com.ie.cloud.payment.dto.MerchantOrderRecordAdd;
import com.ie.cloud.payment.dto.PayDetailQueryDto;
import com.ie.cloud.payment.service.impl.PayOrderFlowRecordServiceImpl;
import com.ie.cloud.payment.service.impl.PayOrderService;
import com.ie.cloud.payment.vo.ActivatePaymentResponseVo;
import com.ie.cloud.payment.vo.MyBillSingleQueryResponseVo;
import com.ie.cloud.payment.vo.MyPayDetailQueryResponseVo;
import com.ie.cloud.payment.vo.base.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created with FR
 *
 * @Author: DQ
 * @Date: 2025/03/03/14:46
 * @Description: 描述语句
 */
@Tag(name = "订单管理")
@RestController
@RequestMapping("/api/myPayOrder")
@Validated
public class PayOrderController extends CommonCtrl {

    @Resource
    private PayOrderService payOrderService;

    @Resource
    private PayOrderFlowRecordServiceImpl payOrderRecordService;


    /**
     * 创建订单
     *
     * @author terrfly
     */
    @Operation(summary = "创建订单")
//    @PreAuthorize("hasAuthority('ENT_PAY_ORDER_CREATE')")
    @PostMapping("/create")
    @MethodLog(remark = "创建订单")
    public ResponseEntity<Result<JSONObject>> createOrder(@Validated @RequestBody MerchantOrderRecordAdd add) {
        return ResponseEntity.ok(payOrderService.generateAnOrder(add));
    }


    /**
     * 导入账单
     */
    @Operation(summary = "导入账单")
    @PostMapping("/import-bill")
    @MethodLog(remark = "导入账单")
    public ResponseEntity<Result<Integer>> importBill(@Validated @RequestBody MerchantOrderRecordAdd add, @RequestParam("payOrderId") String payOrderId) {
        return ResponseEntity.ok(payOrderService.billLoadRequest(payOrderId, add));
    }

    /**
     * 缴费大厅账单
     */
    @Operation(summary = "缴费大厅账单")
    @PostMapping("/hall-pay-bill")
    public ResponseEntity<String> EPaymentBillRequestV1(
            @RequestParam("payOrderId") String payOrderId,
            @RequestParam("appId") String appId,
            @RequestParam("userId") String userId) {
        String htmlContent = payOrderService.billRequestV1Function(payOrderId, appId, userId);
        return ResponseEntity.ok()
                .header("Content-Type", "text/html; charset=UTF-8")
                .body(htmlContent);
    }


    @Operation(summary = "缴费平台单笔账单查询")
    @PostMapping("/bill-single-query")
    @MethodLog(remark = "缴费平台单笔账单查询")
    public ResponseEntity<Result<MyBillSingleQueryResponseVo>> billSingleQuery(
            @RequestParam("appId") String appId,
            @RequestParam("orderId") String orderId) {
        return ResponseEntity.ok(payOrderService.billSingleQuery(appId, orderId));
    }


    /**
     * 缴费明细查询
     */
    @Operation(summary = "缴费明细查询")
    @PostMapping("/payment-detail-query")
    @MethodLog(remark = "缴费明细查询")
    public ResponseEntity<Result<MyPayDetailQueryResponseVo>> paymentDetailQuery(
            @Validated @RequestBody PayDetailQueryDto dto) {
        return ResponseEntity.ok(payOrderRecordService.paymentDetailQuery(dto));
    }


    @Operation(summary = "生成订单=>导入账单=>唤起支付")
    @PostMapping("/create-import-pay")
    @MethodLog(remark = "生成订单=>导入账单=>唤起支付")
    public
    ResponseEntity
            <Result<ActivatePaymentResponseVo<MerchantOrderRecordAdd>>> createOrderImportPay(
            @Validated @RequestBody MerchantOrderRecordAdd add,
            HttpServletRequest request) {


        add.setClientIp(ServletUtil.getClientIP(request));
        return ResponseEntity.ok(payOrderService.activatePayment(add));

    }
}
